package com.frxuexi.mine.view

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.RoutePath
import com.frxuexi.lib_base.UiState
import com.frxuexi.mine.intent.UserIntent
import com.frxuexi.mine.viewmodel.UserViewModel
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch

@Composable
fun LoginPage(navHostController: NavHostController,vm:UserViewModel= hiltViewModel()) {
    var uname by remember {
        mutableStateOf("1001")
    }
    var pass by remember {
        mutableStateOf("666666")
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        MMKV.defaultMMKV().encode("token",it.data.toString())
                        MMKV.defaultMMKV().encode("uname",uname)
                        navHostController.popBackStack()
                    }

                    else -> {}
                }
            }
        }
    })
    Column(Modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(20.dp)) {
        BaseTitle(title = "登录", navHostController = navHostController)
        val modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 20.dp)
        OutlinedTextField(value = uname, modifier = modifier, onValueChange = {uname=it}, label = { Text(text = "用户名")})
        OutlinedTextField(value = pass,modifier = modifier, onValueChange = {pass=it}, label = { Text(text = "密码")},
            visualTransformation=PasswordVisualTransformation())
        Button(modifier = modifier,onClick = {
            if(uname.isNullOrEmpty() || pass.isNullOrEmpty()){
                ToastUtils.showLong("输入内容不能为空")
                return@Button
            }
//            if(RegexUtils.isUsername())
            if(!uname.matches(Regex("[A-z0-9_]{3,10}"))){
//            if(!uname.matches(Regex("1[0-9]{10}"))){
                ToastUtils.showLong("用户名不合法")
                return@Button
            }

            vm.sendIntent(UserIntent.login(mapOf("uname" to uname,"upass" to pass)))
        }) {
            Text(text = "登录")
        }
        Text(text = "没有账号？去注册",
            Modifier
                .padding(start = 20.dp)
                .clickable {
                    navHostController.navigate(RoutePath.REGISTER)
//                    navHostController.navigate(RoutePath.ROOMREGISTER)
                })
    }
}